﻿/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    ListNode* pcur = headA;
    int count1 = 0;
    int count2 = 0;
    while (pcur) {
        count1++;
        pcur = pcur->next;
    }
    pcur = headB;
    while (pcur) {
        count2++;
        pcur = pcur->next;
    }
    int gap = abs(count2 - count1);
    ListNode* longList = headA;
    ListNode* shortList = headB;
    if (count1 < count2) {
        longList = headB;
        shortList = headA;
    }
    while (gap--) {
        longList = longList->next;
    }
    while (longList) {
        if (longList == shortList) {
            return longList;
        }
        longList = longList->next;
        shortList = shortList->next;
    }
    return NULL;
}